<?php !defined( 'SUNCMS' ) && exit( 'No direct script access allowed'  );

/**
 * 
 * Sun CMS
 * 
 * @author			Burning
 * @copyright		Copyright (c) 2009, IsunCms.Com.
 * @license			http://www.isuncms.com/license.htm
 * @link				http://www.isuncms.com/
 * @since				Version 1.0 Beta
 * 
 */

class Article
{

	var $db;
	
	function __construct()
	{
		global $db;
		$this->db = $db;
	}
	
	/**
	 * 获取文章数
	 *
	 * @param string $type
	 */
	function get_article_no( $type, $id = 0 )
	{
		switch( $type )
		{
			case 'col':
				$where = array( 'cid' => $id, 'draft' => 0, 'recycle' => 0 );
				break;
			case 'recycle':
				$where = array( 'recycle' => '1' );
				break;
			case 'draft':
				$where = array( 'draft' => '1' );
				break;
			case 'vali':
				$where = array( 'validate' => '0' );
				break;
			default:
				$where = array( 'recycle' => '0', 'draft' => '0' );
		}
		$result = $this->db->count( DB_PRE.'article', $where );
		return $result;
	}
	
	/**
	 * 获取文章列表
	 *
	 * @param int $start
	 * @param string $type
	 * @return array
	 */
	function get_article_list( $start, $type, $id )
	{
		switch( $type )
		{
			case 'recycle':
				$where = "WHERE `a`.`recycle` = '1'";
				break;
			case 'draft':
				$where = "WHERE `a`.`draft` = '1'";
				break;
			case 'all':
				$where = "WHERE `a`.`draft` = '0' AND `a`.`recycle` = '0'";
				break;
			case 'vali':
				$where = "WHERE `a`.`validate` = '0'";
				break;
			case 'col':
				$where = "WHERE `a`.`cid` = '$id' AND `a`.`draft` = '0' AND `a`.`recycle` = '0'";
				break;
		}
		$sql = "SELECT `a`.`id`, `a`.`title`, `a`.`author`, `a`.`small_title`, `a`.`page_no`, `a`.`recommend`, `a`.`title_color`, `a`.`validate`, `a`.`post_time` AS `date`, `b`.`name` AS `category` FROM `".DB_PRE."article` as `a` LEFT JOIN `".DB_PRE."articlecolumn` as `b` ON `b`.`id`= `a`.`cid` ".$where." ORDER BY `a`.`post_time` DESC LIMIT $start, 15";
		$result = $this->db->query( $sql );
		if( !empty( $result ) )
		{
			foreach( $result as $key => $value )
			{
				$result[$key]['title'] = $result[$key]['small_title'] == ''?$result[$key]['title']:$result[$key]['small_title'];
				$result[$key]['date'] = substr( $result[$key]['date'], 0, 10 );
			}
			return $result;
		}
		return array();
	}
	
	/**
	 * 获取分类
	 *
	 * @return array
	 */
	function get_category( $count_ar_no = false )
	{
		$category = cache_read( 'article', 'category' );
		if( $count_ar_no )
		{
			foreach( $category as $key => $value )
			{
				$category[$key]['article_no'] = $this->db->count( DB_PRE.'article', array( 'cid' => $value['id'], 'draft' => 0 ) );
			}
		}
		return $category;
	}
	
	/**
	 * 搜索文章
	 *
	 * @param int $start
	 * @param string $rule
	 * @param string $key
	 * @return array
	 */
	function search_article( $start, $rule, $key )
	{
		if( $rule == 'tags' )
		{
			$ids_sql = 'SELECT `aid` FROM `'.DB_PRE.'tags` WHERE `name` = "'.$key.'"';
			$ids = $this->db->query( $ids_sql );
			$ids_no = count( $ids );
			if( $ids_no == 0 )
			{
				return array( 'articleNo' => 0, 'articles' => array() );
			}
			elseif( $ids_no > 20 )
			{
				$ids2 = $ids;
				unset( $ids );
				foreach( $ids2 as $k => $v )
				{
					if( $k >= $start && $k <= $start+20 )
					{
						$ids[] = $v;
					}
				}
			}
			$articles = array();
			foreach( $ids as $k2 => $v2 )
			{
				$sql = "SELECT `a`.`id`, `a`.`title`, `a`.`author`, `a`.`small_title`, `a`.`title_color`, `a`.`page`, `a`.`validate`, `a`.`add_time` AS `date`, `b`.`name` AS `category` FROM `".DB_PRE."article` as `a` LEFT JOIN `".DB_PRE."articlecolumn` as `b` ON `b`.`id`= `a`.`cid` WHERE `a`.`id` = {$v2['aid']}";
				$article = $this->db->query( $sql, true );
				$article['title'] = $result[$key]['small_title'] == ''?$result[$key]['title']:$result[$key]['small_title'];
				$article['date'] = substr( $result[$key]['date'], 0, 10 );
				$articles[] = $article;
				unset( $sql, $articles, $article );
			}
			return array( 'articleNo' => $ids_no, 'articles' => $articles );
		}
		switch( $rule )
		{
			case 'author':
				$count_sql = 'SELECT COUNT(`id`) AS `no` FROM `'.DB_PRE.'article` WHERE `author` = "'.$key.'"';
				$where = 'WHERE `a`.`author` = "'.$key.'"';
				break;
			case 'title':
				$count_sql = 'SELECT COUNT(`id`) AS `no` FROM `'.DB_PRE.'article` WHERE `title` LIKE "%'.$key.'%"';
				$where = 'WHERE `a`.`title` LIKE "%'.$key.'%"';
				break;
			case 'data':
				$count_sql = 'SELECT COUNT(`id`) AS `no` FROM `'.DB_PRE.'article` WHERE `add_time` LIKE "'.$key.'%"';
				$where = 'WHERE `a`.`add_time` LIKE "'.$key.'%"';
				break;
		}
		$article_no = $this->db->query( $count_sql, true );
		$article_no = $article_no['no'];
		if( $article_no == 0 )
		{
			return array( 'articleNo' => 0, 'articles' => array() );
		}
		$sql = "SELECT `a`.`id`, `a`.`title`, `a`.`author`, `a`.`small_title`, `a`.`title_color`, `a`.`validate`, `a`.`add_time` AS `date`, `a`.`page`, `b`.`name` AS `category` FROM `".DB_PRE."article` as `a` LEFT JOIN `".DB_PRE."articlecolumn` as `b` ON `b`.`id`= `a`.`cid` ".$where." ORDER BY `a`.`add_time` DESC LIMIT $start, 20";
		$result = $this->db->query( $sql );
		foreach( $result as $key => $value )
		{
			$result[$key]['title'] = $result[$key]['small_title'] == ''?$result[$key]['title']:$result[$key]['small_title'];
			$result[$key]['date'] = substr( $result[$key]['date'], 0, 10 );
		}
		return array( 'articleNo' => $article_no, 'articles' => $result );
	}
	
	/**
	 * 获取专辑列表
	 *
	 */
	function get_special_list()
	{
		$sql = "SELECT `id`, `name` FROM `".DB_PRE."special`";
		return $this->db->query( $sql );
	}
	
	/**
	 * 获取来源列表
	 *
	 */
	function get_from_list()
	{
		$data = file_get_contents( SUN_CACHE.'article/from.txt' );
		$from_array = explode( '|', $data );
		$from = array();
		foreach( $from_array as $v )
		{
			if( !empty( $v ) )
			{
				$from[] = $v;
			}
		}
		return $from;
	}
	
	/**
	 * 获取作者列表
	 *
	 * @return unknown
	 */
	function get_author_list()
	{
		$data = file_get_contents( SUN_CACHE.'article/author.txt' );
		$author_array = explode( '|', $data );
		$author = array();
		foreach( $author_array as $v )
		{
			if( !empty( $v ) )
			{
				$author[] = $v;
			}
		}
		return $author;
	}
	
	/**
	 * 获取文章标题
	 *
	 * @param int $id
	 */
	function get_article_title( $id )
	{
		$sql = "SELECT `title` FROM `".DB_PRE."article` WHERE `id` =  '{$id}'";
		$result = $this->db->query( $sql, true );
		return $result['title'];
	}
	
	/**
	 * 添加续发文章到数据库
	 *
	 * @param string $datas
	 * @param int $id
	 * @return bool
	 */
	function add_more( $datas, $id )
	{
		//是否下载非站内图片
		if( $datas['down_pic'] )
		{
			$content = get_pic( $datas['content'], $datas['down_pic'], $get_first_pic );
		}
		//对关键词进行处理
		$content = add_keywords( $datas['content'] );
		//判断是否需要分页
		if( strstr( $content, '[-page-]' ) )
		{
			$content = explode_content( $content );
		}
		else 
		{
			$content = array( $content );
		}
		$result = $this->insert_article_content( $content, $id, true );
		return ( $result )?true:false;
	}
	
	/**
	 * 添加文章内容到数据库
	 *
	 * @param array $content
	 * @param int $id
	 */
	function insert_article_content( $content, $id, $type = false )
	{
		if( $type )
		{
			$sql = "SELECT `page` FROM `".DB_PRE."article` WHERE `id` = '$id'";
			$result = $this->db->query( $sql, true );
			$page = $result['page'];
		}
		else 
		{
			$page = 0;
		}
		$i = 0;
		$all_page = $page+1;
		foreach( $content as $k => $v )
		{
			$v = str_replace( '"', "'", $v );
			$now_page = $page+$k+1;
			$datas = array( 'aid' => $id, 'content' => $v, 'page' => $now_page );
			if( !$this->db->insert( DB_PRE.'articlecontent', $datas ) )
			{
				$i = 1;
			}
			$all_page = $all_page+$k;
			unset( $v, $now_page, $datas );
		}
		$i == 0?$this->db->update( DB_PRE.'article', array( 'page' => $all_page ), array( 'id' => $id ) ):'';
		return $i == 0?true:false;
	}
	
	/**
	 * 获取文章页数
	 *
	 * @param int $id
	 * @return bool
	 */
	function get_article_page( $id )
	{
		$sql = "SELECT `id`, `page` FROM `".DB_PRE."articlecontent` WHERE `aid` = '$id' ORDER BY `page` ASC";
		$result = $this->db->query( $sql );
		return $result;
	}
	
	/**
	 * 获取文章详细
	 *
	 * @param int $id
	 * @param int $page
	 * @param bool $type
	 * @return array
	 */
	function get_article( $id, $page, $type = false )
	{
		if ( $type )
		{
			$sql_basic = "SELECT `cid`, `title`, `small_title`, `tags`, `keywords`,`author`, `from`, `index_top`, `column_top`, `recommend`, `title_color`, `sid`, `description`, `url_jump`, `comment`, `money`, `add_time`, `validate`, `password`, `thumb_pic`, `draft`, `page` FROM `".DB_PRE."article` WHERE `id` = '$id'";
			$ar_basic =  $this->db->query( $sql_basic, true );
			$ar_content = '';
			if( $page == 0 )
			{
				$sql = "SELECT `content` FROM `".DB_PRE."articlecontent` WHERE `aid` = '$id' ORDER BY `page` ASC";
				$result = $this->db->query( $sql );
				foreach( $result as $k => $v )
				{
					if( $k+1 == $ar_basic['page'] )
					{
						$ar_content .= $v['content'];
					}
					else
					{
						$ar_content .= $v['content'].'[-page-]';
					}
				}
			}
			else if( $page == 1 )
			{
				$sql = "SELECT `content` FROM `".DB_PRE."articlecontent` WHERE `aid` = '$id' AND `page` = '1'";
				$result = $this->db->query( $sql, true );
				$ar_content .= $result['content'];
			}
			return array( 'ar_basic' => $ar_basic, 'ar_content' => $ar_content );
		}
		else
		{
			$sql = "SELECT `content` FROM `".DB_PRE."articlecontent` WHERE `aid` = '$id' AND `page` = '$page'";
			$content = $this->db->query( $sql, true );
			return $content['content'];
		}
	}
	
	/**
	 * 获取一篇完整的文章
	 *
	 * @param int $id
	 * @param int $page
	 * @return array
	 */
	function get_article_one( $id, $page )
	{
		$sql = "SELECT `a`.`title`, `a`.`tags`, `a`.`author`, `a`.`from`, `a`.`title_color`, `a`.`page`, `a`.`description`, `a`.`add_time`, `b`.`content` FROM `".DB_PRE."article` AS `a` LEFT JOIN `".DB_PRE."articlecontent` AS `b` ON `a`.`id` = `b`.`aid` AND `b`.`page` = '$page' WHERE `a`.`id` = '$id'";
		$articles = $this->db->query( $sql, true );
		return $articles;
	}
	
	function article_validate( $id, $datas )
	{
		$msg = '您的文章《'.$datas['title'].'》';
		if( $datas['validate'] == 1 )
		{
			if( !empty( $datas['setting'] ) )
			{
				if( isset( $datas['setting']['best'] ) )
				{
					$msg .= '被加精';
				}
			}
		}
		else 
		{
			$msg .= '审核未通过！';
		}
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	/**
	 * 获取评论
	 *
	 * @param string $type
	 * @param int $start
	 * @param string $data
	 * @return array
	 */
	function get_comment( $type, $start, $data = '' )
	{
		switch( $type )
		{
			case 'unvalidate':
				$where = 'WHERE `a`.`validate`=0';
			case 'article':
				$where = 'WHERE `a`.`aid` = '.$data;
			case 'date':
				$where = 'WHERE `a`.`date` LIKE "'.$data.'%"';
		}
		$sql = 'SELECT `a`.`id`, `a`.`title`, `a`.`add_time`, `b`.`title` as `ar_title`, `b`.`id` as `ar_id` FROM `'.DB_PRE.'articlecomment` as `a`
				LEFT JOIN `'.DB_PRE.'article` as `b` ON `a`.`aid` = `b`.`id` '.$where.' ORDER BY `a`.`id` LIMIT '.$start.', 20';
		$result = $this->db->query( $sql );
		return $result;
	}
	
	/**
	 * 获取回复数
	 *
	 * @param string $type
	 * @param string $data
	 * @return int
	 */
	function get_comment_no( $type, $data )
	{
		switch( $type )
		{
			case 'unvalidate':
				$where = 'WHERE `validate` = 0';
			case 'article':
				$where = 'WHERE `aid` = '.$data;
			case 'date':
				$where = 'WHERE `date` LIKE "'.$data.'%"';
		}
		$sql = 'SELECT COUNT(`id`) as `no` FROM `'.DB_PRE.'articlecomment` '.$where;
		$result = $this->db->query( $sql, true );
		return $result['no'];
	}
	
	/**
	 * 验证回复
	 *
	 * @param string $type
	 * @param string $ids
	 */
	function validate_comment( $type, $ids )
	{
		$id_array = explode( '_', $ids );
		foreach( $id_array as $v )
		{
			if( !empty( $v ) )
			{
				if( $type == 'validate' )
				{
					$this->db->update( DB_PRE.'articlecomment', array( 'validate' => 1 ), array( 'id' => $v ) );
				}
				elseif( $type == 'unvalidate' )
				{
					$this->db->update( DB_PRE.'articlecomment', array( 'validate' => 0 ), array( 'id' => $v ) );
				}
			}
		}
		return;
	}
	
	/**
	 * 检查重复标题
	 *
	 * @param string $str
	 * @return bool
	 */
	function check_title( $str )
	{
		$result =  $this->db->get_where( DB_PRE.'article', array( 'title' => $str ) );
		return empty( $result )?true:false;
	}
	
	
}


class Pubarticle{

	var $db;
	
	function __construct()
	{
		global $db;
		$this->db = $db;
	}
	



	
	
	function get_article_one( $id, $page )
	{
		$sql = "SELECT `a`.`title`, `a`.`small_title`, `a`.`tags`, `a`.`author`, `a`.`from`, `a`.`title_color`, `a`.`description`, `a`.`add_time`, `b`.`content` FROM `".DB_PRE."article` AS `a` LEFT JOIN `".DB_PRE."articlecontent` AS `b` ON `a`.`cid` = `b`.`id` WHERE `a`.`id` = '$id'";
		$articles = $this->db->query( $sql, true );
		return $articles;
	}
	

	
	/**
	 * 获取专辑列表
	 *
	 */
	function get_special_list()
	{
		$sql = "SELECT `id`, `name` FROM `".DB_PRE."special`";
		return $this->db->query( $sql );
	}
	
	/**
	 * 获取来源列表
	 *
	 */
	function get_from_list()
	{
		$data = file_get_contents( SUN_CACHE.'article/from.txt' );
		$from_array = explode( '|', $data );
		$from = array();
		foreach( $from_array as $v )
		{
			if( !empty( $v ) )
			{
				$from[] = $v;
			}
		}
		return $from;
	}
	
	/**
	 * 获取作者列表
	 *
	 * @return unknown
	 */
	function get_author_list()
	{
		$data = file_get_contents( SUN_CACHE.'article/author.txt' );
		$author_array = explode( '|', $data );
		$author = array();
		foreach( $author_array as $v )
		{
			if( !empty( $v ) )
			{
				$author[] = $v;
			}
		}
		return $author;
	}
	
	
	
	/**
	 * 获取文章标题
	 *
	 * @param int $id
	 */
	function get_article_title( $id )
	{
		$sql = "SELECT `title` FROM `".DB_PRE."article` WHERE `id` =  '{$id}'";
		$result = $this->db->query( $sql, true );
		return $result['title'];
	}
	
	/**
	 * 对文章进行处理并插入到数据库
	 *
	 * @param array $datas
	 * @return bool
	 */
	function add_article( $ar_basic, $ar_content )
	{
		include( SUN_FUNC.'charset.php' );
		$get_first_pic = ( empty( $ar_basic['thumb_pic'] ) && $ar_content['get_first_pic'] )?true:false;
		//如果description为空，自动提取description
		if( empty( $ar_basic['description'] ) )
		{
			$contents = str_replace( '[-page-]', '', $ar_content['content'] );
			$ar_basic['description'] = $this->my_substr( strip_tags( $contents ), 300 );
		}
		//如果keywords为空，则自动提取keywords
		if( empty( $ar_basic['keywords'] ) )
		{
			$ar_basic['keywords'] = $this->get_tags( $ar_basic['title'], $ar_basic['description'] );
		}
		else 
		{
			$ar_basic['keywords'] = str_replace( '，', ',', $ar_basic['keywords'] );
		}
		//如果tags为空，则将keywords转化为tags
		if( empty( $ar_basic['tags'] ) )
		{
			$ar_basic['tags'] = $ar_basic['keywords'];
		}
		else 
		{
			$ar_basic['tags'] = str_replace( '，', ',', $ar_basic['tags'] );
		}
		//是否下载非站内图片以及提取内容中第一个图片为缩略图
		if( $ar_content['down_pic'] || $get_first_pic )
		{
			$result = $this->get_pic( $ar_content['content'], $ar_content['down_pic'], $get_first_pic );
			$get_first_pic?$ar_basic['thumb_pic'] = $result['thumb_pic']:'';
			$ar_content['content'] = $result['content'];
		}
		//对关键词进行处理
		$content = $this->add_keywords( $ar_content['content'] );
		//判断是否需要分页
		if( strstr( $content, '[-page-]' ) )
		{
			$content = $this->explode_content( $content );
		}
		else 
		{
			$content = array( $content );
		}
		//判断文章基本数据是否插入到数据库
		if( !$this->db->insert( DB_PRE.'article', $ar_basic ) )
		{
			return false;
		}
		$id = $this->db->insert_id();
		//处理tags
		$this->ins_upt_tags( $ar_basic['tags'], $id );
		//判断内容是否插入到数据库
		if( !$this->insert_article_content( $content, $id ) )
		{
			$this->del_one_article( $id );
			return false;
		}
		return array( 'id' => $id, 'keywords' => $ar_basic['keywords'], 'tags' => $ar_basic['tags'], 'description' => $ar_basic['description'] );
	}
	
	/**
	 * 添加续发文章到数据库
	 *
	 * @param string $datas
	 * @param int $id
	 * @return bool
	 */
	function add_more( $datas, $id )
	{
		//是否下载非站内图片
		if( $datas['down_pic'] )
		{
			$content = $this->get_pic( $datas['content'], $datas['down_pic'], $get_first_pic );
		}
		//对关键词进行处理
		$content = $this->add_keywords( $datas['content'] );
		//判断是否需要分页
		if( strstr( $content, '[-page-]' ) )
		{
			$content = $this->explode_content( $content );
		}
		else 
		{
			$content = array( $content );
		}
		$result = $this->insert_article_content( $content, $id, true );
		return ( $result )?true:false;
	}
	
	/**
	 * 添加文章内容到数据库
	 *
	 * @param array $content
	 * @param int $id
	 */
	function insert_article_content( $content, $id, $type = false )
	{
		if( $type )
		{
			$page = $this->db->count( DB_PRE.'articlecontent', array( 'aid' => $id ) );
		}
		else
		{
			$page = 0;
		}
		$i = 0;
		foreach( $content as $k => $v )
		{
			$v = str_replace( '"', "'", $v );
			$now_page = $page+$k+1;
			$datas = array( 'aid' => $id, 'content' => $v, 'page' => $now_page );
			if( !$this->db->insert( DB_PRE.'articlecontent', $datas ) )
				$i = 1;
			unset( $now_page );
		}
		return ( $i == 0 )?true:false;
	}
	
	
	/**
	 * 更新文章数据到数据库
	 *
	 * @param int $id
	 * @param array $datas
	 * @param int $pg
	 * @return bool
	 */
	function update_ar( $id, $ar_basic, $ar_content, $page )
	{
		include( SUN_FUNC.'charset.php' );
		if(  !empty( $ar_basic['thumb_pic'] ) && !empty( $ar_content['old_thumb_pic'] ) && ( $ar_content['old_thumb_pic'] != $ar_basic['thumb_pic'] ) )
		{
			@unlink( SUNCMS.'uploads/thumb/'.$ar_content['old_thumb_pic'] );
		}
		$get_first_pic = ( empty( $ar_basic['thumb_pic'] ) && $ar_content['get_first_pic'] )?true:false;
		//如果description为空，自动提取description
		if( empty( $ar_basic['description'] ) )
		{
			$contents = str_replace( '[-page-]', '', $ar_content['content'] );
			$ar_basic['description'] = $this->my_substr( strip_tags( $contents ), 300 );
		}
		//如果keywords为空，则自动提取keywords
		if( empty( $ar_basic['keywords'] ) )
		{
			$ar_basic['keywords'] = $this->get_tags( $ar_basic['title'], $ar_basic['description'] );
		}
		else 
		{
			$ar_basic['keywords'] = str_replace( '，', ',', $ar_basic['keywords'] );
		}
		//如果tags为空，则将keywords转化为tags
		if( empty( $ar_basic['tags'] ) )
		{
			$ar_basic['tags'] = $ar_basic['keywords'];
		}
		else 
		{
			$ar_basic['tags'] = str_replace( '，', ',', $ar_basic['tags'] );
		}
		//是否下载非站内图片以及提取内容中第一个图片为缩略图
		if( $ar_content['down_pic'] || $get_first_pic )
		{
			$result = $this->get_pic( $ar_content['content'], $ar_content['down_pic'], $get_first_pic );
			$get_first_pic?$ar_basic['thumb_pic'] = $result['thumb_pic']:'';
			$ar_content['content'] = $result['content'];
		}
		//对关键词进行处理
		$content = $this->add_keywords( $ar_content['content'] );
		//判断是否需要分页
		if( strstr( $content, '[-page-]' ) )
		{
			$content = $this->explode_content( $content );
		}
		else 
		{
			$content = array( $content );
		}
		//处理tags
		$this->ins_upt_tags( $ar_basic['tags'], $id, true );
		//判断文章基本数据是否插入到数据库
		if( !$this->db->update( DB_PRE.'article', $ar_basic, array( 'id' => $id ) ) )
		{
			return false;
		}
		//判断编辑模式
		if( $page == 0 )
		{
			$this->db->delete( DB_PRE.'articlecontent', array( 'aid' => $id ) );
			if( !$this->insert_article_content( $content, $id ) )
			{
				$this->del_one_article( $id );
				return false;
			}
		}
		else
		{
			$this->db->delete( DB_PRE.'articlecontent', array( 'aid' => $id, 'page' => 1 ) );
			//计算页数
			$contentNo = count( $content );
			//是否需要更新内容其他页页数
			if( $contentNo > 1 )
			{
				$this->update_article_ohter_page( $contentNo-1, 1, $id );
			}
			if( !$this->update_article_content( $content, $id, 1, $contentNo ) )
			{
				return false;
			}
		}
		return array( 'tags' => $ar_basic['tags'], 'keywords' => $ar_basic['keywords'], 'description' => $ar_basic['description'] );
	}
	
	function update_content( $id, $page, $datas )
	{
		$content = $datas['content'];
		//是否下载非站内图片以及提取内容中第一个图片为缩略图
		if( $datas['down_pic'] )
		{
			$result = $this->get_pic( $content, $datas['down_pic'], false );
			$content = $result['content'];
		}
		//判断是否需要分页
		if( strstr( $content, '[-page-]' ) )
		{
			$content = $this->explode_content( $content );
		}
		else 
		{
			$content = array( $content );
		}
		$this->db->delete( DB_PRE.'articlecontent', array( 'aid' => $id, 'page' => $page ) );
		//计算页数
		$contentNo = count( $content );
		//是否需要更新内容其他页页数
		if( $contentNo > 1 )
		{
			$this->update_article_ohter_page( $contentNo-1, $page, $id );
		}
		if( !$this->update_article_content( $content, $id, $page, $contentNo ) )
		{
			return false;
		}
		return true;
	}
	
	/**
	 *  * 更新内容其他页页数
	 *
	 * @param int $pageNo（总页数）
	 * @param int $pg	（当前页）
	 * @param int $id	（当前文章id）
	 */
	function update_article_ohter_page( $pageNo, $page, $id )
	{
		$sql = "UPDATE `".DB_PRE."articlecontent` SET `page` = `page`+$pageNo WHERE `aid` = $id AND `page` > $page";
		$this->db->execute( $sql );
		return;
	}
	
	/**
	 * 更新文章内容
	 *
	 * @param array $datas
	 * @param int $id
	 * @param int $pg
	 * @return bool
	 */
	function update_article_content( $contents, $id, $page, $pageNo )
	{
		$i = 0;
		foreach( $contents as $k => $v )
		{
			$v = str_replace( '"', '\'', $v );
			$datas = array( 'aid' => $id, 'content' => $v, 'page' => $page+$k );
			if( !$this->db->insert( DB_PRE.'articlecontent', $datas ) )
			{
				$i = 1;
			}
			unset( $datas );
		}
		return ( $i == 0 )?true:false;
	}
	
	/**
	 * 更新文章页面排序
	 *
	 * @param int $id
	 * @param array $datas
	 * @return bool
	 */
	function update_article_page( $id, $datas )
	{
		$i = 0;
		foreach( $datas as $k => $v )
		{
			$sql = "UPDATE `".DB_PRE."articlecontent` SET `page` = $v WHERE `aid` = $id AND `page` = $k";
			if( !$this->db->execute( $sql ) )
				$i = 1;
		}
		return ( $i == 0 )?true:false;
	}
	
	/**
	 * 删除文章页面
	 *
	 * @param int $id
	 * @param int $pg
	 * @return bool
	 */
	function del_page( $id, $pg )
	{
		$delete = $this->db->delete( 'article_content', array( 'aid' => $id, 'page' => $pg ) );
		$sql = "UPDATE `".DB_PRE."articlecontent` SET `page` = `page`-1 WHERE `aid` = '$id' AND `page` > '$pg'";
		$update = $this->db->execute( $sql );
		return ( $delete && $update )?true:false;
	}
	
	/**
	 * 回收与还原文章
	 *
	 * @param int $articleId
	 * @return bool
	 */
	function recycle_article( $articleId )
	{
		$sql = "SELECT `recycle` FROM `".DB_PRE."article` WHERE `id` = $articleId";
		$result = $this->db->query( $sql, 'row' );
		$data = array( 'recycle' => $result['recycle'] == 1?0:1, 'draft' => $result['recycle'] == 1?1:0 );
		return ( $this->db->update( DB_PRE.'article', $data, array( 'id' => $articleId ) ) )?true:false;
	}
	
	/**
	 * 从草稿箱中发布文章
	 *
	 * @param int $articleId
	 * @return bool
	 */
	function draft_article( $articleId )
	{
		$sql = "SELECT `draft` FROM `".DB_PRE."article` WHERE `id` = $articleId";
		$result = $this->db->query( $sql, 'row' );
		return ( $this->db->update( DB_PRE.'article', array( 'draft' => 0 ), array( 'id' => $articleId ) ) )?true:false;
	}
	
	/**
	 * 删除一篇文章
	 *
	 * @param int $id
	 */
	function del_one_article( $id )
	{
		$del_basic = $this->db->delete( DB_PRE.'article', array( 'id' => $id ) );
		$del_content = $this->db->delete( DB_PRE.'articlecontent', array( 'aid' => $id ) );
		$del_tags = $this->db->delete( DB_PRE.'tags', array( 'aid' => $id ) );
		return ( $del_basic && $del_content && $del_tags )?true:false;
	}
	
	/**
	 * 搜索文章
	 *
	 * @param int $start
	 * @param string $rule
	 * @param string $key
	 * @return array
	 */
	function search_article( $start, $rule, $key )
	{
		if( $rule == 'tags')
		{
			$ids_sql = 'SELECT `aid` FROM `'.DB_PRE.'tags` WHERE `name` = "'.$key.'"';
			$ids = $this->db->query( $ids_sql );
			$ids_no = count( $ids );
			if( $ids_no == 0 )
			{
				return array( 'articleNo' => 0, 'articles' => array() );
			}
			elseif( $ids_no > 20 )
			{
				$ids2 = $ids;
				unset( $ids );
				foreach( $ids2 as $k => $v )
				{
					if( $k >= $start && $k <= $start+20 ){
						$ids[] = $v;
					}
				}
			}
			$articles = array();
			foreach( $ids as $k2 => $v2 )
			{
				$sql = "SELECT `a`.`id`, `a`.`title`, `a`.`author`, `a`.`small_title`, `a`.`title_color`, `a`.`validate`, `a`.`add_time`, `b`.`name` FROM `".DB_PRE."article` as `a` LEFT JOIN `".DB_PRE."articlecolumn` as `b` ON `b`.`id`= `a`.`cid` WHERE `a`.`id` = {$v2['aid']}";
				$article = $this->db->query( $sql, true );
				$article2 = array
				(
						'id' => $article['id'],
						'title' => $article['small_title'] == ''?$article['title']:$article['small_title'],
						'title_color' => $article['title_color'],
						'page' => $this->db->count( 'article_content', array( 'aid' => $article['id'] ) ),
						'validate' => $article['validate'],
						'date' => substr( $article['add_time'], 0, 10),
						'category' => $article['name'],
						'author' => $article['author']
				);
				$articles[] = $article2;
				unset( $sql, $article, $article2 );
			}
			return array( 'articleNo' => $ids_no, 'articles' => $articles );
		}
		switch( $rule )
		{
			case 'author':
				$count_sql = 'SELECT COUNT(`id`) AS `no` FROM `'.DB_PRE.'article` WHERE `author` = "'.$key.'"';
				$where = 'WHERE `a`.`author` = "'.$key.'"';
				break;
			case 'title':
				$count_sql = 'SELECT COUNT(`id`) AS `no` FROM `'.DB_PRE.'article` WHERE `title` LIKE "%'.$key.'%"';
				$where = 'WHERE `a`.`title` LIKE "%'.$key.'%"';
				break;
			case 'data':
				$count_sql = 'SELECT COUNT(`id`) AS `no` FROM `'.DB_PRE.'article` WHERE `add_time` LIKE "'.$key.'%"';
				$where = 'WHERE `a`.`add_time` LIKE "'.$key.'%"';
				break;
		}
		$article_no = $this->db->query( $count_sql, true );
		$article_no = $article_no['no'];
		if( $article_no == 0 )
		{
			return array( 'articleNo' => 0, 'articles' => array() );
		}
		$sql = "SELECT `a`.`id`, `a`.`title`, `a`.`author`, `a`.`small_title`, `a`.`title_color`, `a`.`validate`, `a`.`add_time`, `b`.`name` FROM `".DB_PRE."article` as `a` LEFT JOIN `".DB_PRE."articlecolumn` as `b` ON `b`.`id`= `a`.`cid` ".$where." ORDER BY `a`.`add_time` DESC LIMIT $start, 20";
		$result = $this->db->query( $sql );
		$articles = array();
		foreach( $result as $key=> $value )
		{
			$articles[] = array
			(
						'id' => $value['id'],
						'title' => $value['small_title'] == ''?$value['title']:$value['small_title'],
						'title_color' => $value['title_color'],
						'page' => $this->db->count( DB_PRE.'articlecontent', array( 'aid' => $value['id'] ) ),
						'validate' => $value['validate'],
						'date' => substr( $value['add_time'], 0, 10),
						'category' => $value['name'],
						'author' => $value['author']
			);
		}
		return array( 'articleNo' => $article_no, 'articles' => $articles );
	}
	
	/***********************************************************************************************************************************************************/
																	//辅助函数
	/**
	 * 将tags更新/插入到数据库
	 *
	 * @param string $tags
	 * @param int $id
	 */
	function ins_upt_tags( $tags, $id, $update = false )
	{
		if( $update )
		{
			$this->db->delete( DB_PRE.'tags', array( 'aid' => $id ) );
		}
		$tags = trim( str_replace( ',', ' ', $tags ) );
		$tags = explode( ' ', $tags );
		$i = 0;
		foreach( $tags as $v )
		{
			$data = array( 'name' => $v, 'aid' => $id );
			$this->db->insert( DB_PRE.'tags', $data );
			unset( $data );
		}
		return;
	}
	
	/**
	 * 对关键词加链接
	 *
	 * @param string $str
	 */
	function add_keywords( $str )
	{
		$content = $str;
		$config = cache_read( 'article', 'keywords' );
		foreach( $config as $k => $v )
		{
			$content = str_replace( "{$v['name']}", '<a href=\''.$v['url'].'\' target=_bank>'.$v['name'].'</a>', $content );
		}
		return $content;
	}
	
	/**
	 * 获取tags
	 *
	 * @param string $content
	 */
	function get_tags( $title, $content )
	{
		include( SUN_CLASS.'split.php' );
		$this->split = new Split();
		$keywords = '';
		$kws = '';
		$kwsArray = array();
		$titleTags = explode( ' ', @$this->split->GetIndexText( $title ) );
		$contentTags = explode( ' ', @$this->split->GetIndexText( $content ) );
		if( is_array( $titleTags ) && is_array( $contentTags ) )
		{
			foreach( $titleTags as $v )
			{
				if( strlen( $kws.$v ) >= 30 )
				{
					break;
				}
				else
				{
					$kws .= $v.',';
					$kwsArray[] = $v;
				}
			}
			foreach( $contentTags as $v )
			{
				if( strlen( $kws.$v ) >= 30 )
				{
					break;
				}
				else if( !in_array( $v, $titleTags ) )
				{
					$kws .= $v.',';
					$kwsArray[] = $v;
				}
			}
		}
		$this->split->Clear();
		$this->split = null;
		$kwsNo = count( $kwsArray );
		foreach( $kwsArray as $k => $v )
		{
			if( $k+1 == $kwsNo )
			{
				$keywords .= $v;
			}
			else
			{
				$keywords .= $v.',';
			}
		}
		return $keywords;
	}
	
	/**
	 * 字符串截取
	 *
	 * @param string $str
	 * @param int $strlen
	 * @return string
	 */
	function my_substr($string, $length, $havedot=0)
	{
		//判断长度
		if(strlen($string) <= $length)
		{
			return $string;
		}
		$wordscut = '';
		//utf8编码
		$n = 0;
		$tn = 0;
		$noc = 0;
		while ($n < strlen($string))
		{
			$t = ord($string[$n]);
			if($t == 9 || $t == 10 || (32 <= $t && $t <= 126)) 
			{
				$tn = 1;
				$n++;
				$noc++;
			} 
			elseif(194 <= $t && $t <= 223) 
			{
				$tn = 2;
				$n += 2;
				$noc += 2;
			} 
			elseif(224 <= $t && $t < 239) 
			{
				$tn = 3;
				$n += 3;
				$noc += 2;
			} 
			elseif(240 <= $t && $t <= 247) 
			{
				$tn = 4;
				$n += 4;
				$noc += 2;
			} 
			elseif(248 <= $t && $t <= 251) 
			{
				$tn = 5;
				$n += 5;
				$noc += 2;
			} 
			elseif($t == 252 || $t == 253) 
			{
				$tn = 6;
				$n += 6;
				$noc += 2;
			} 
			else 
			{
				$n++;
			}
			if ($noc >= $length) 
			{
				break;
			}
		}
		if ($noc > $length) 
		{
			$n -= $tn;
		}
		$wordscut = substr($string, 0, $n);
		//省略号
		if($havedot) 
		{
			return $wordscut.'...';
		} 
		else 
		{
			return $wordscut.'&nbsp;';
		}
	}
	
	/**
	 * 下载非站内图片并加上水印
	 *
	 * @param string $str
	 * @return string
	 */
	function get_pic( $str, $down_pic, $get_first_pic = false )
	{
		preg_match_all( "/http:\/\/([^ '\"<>]+)\.(gif|jpg|png)/is", $str, $array );
		if( empty( $array[0] ) )
		{
			return $str;
		}
		include( SUN_MODULE.'public/images.php' );
		$this->m_images = new Images();
		$config = cache_read( 'config', 'sysconfig' );
		$urls = array();
		$thumb_pic = '';
		$content = '';
		foreach( $array[0] as $v )
		{
			if( !strstr( $config['base_url'], $v ) )
			{
				$urls[] = $v;
			}
		}
		foreach( $urls AS $key => $value)
		{
			$name_ext = basename( $value );
			$name_exts = explode( '.', $name_ext );
			$name = md5( microtime( true ).$name_exts[0] ).'.'.$name_exts[1];
			$validate = false;
			if( @copy( $value, SUNCMS.'uploads/temp/'.$name ) )
			{
				$validate = true;
				chmod( SUNCMS.'uploads/temp/'.$name, 0777 );
			}
			elseif( $img = file_get_contents( $value ) )
			{
				$validate = true;
				file_put_contents( SUNCMS.'uploads/temp/'.$name, $img );
				chmod( SUNCMS.'uploads/temp/'.$name, 0777 );
			}
			if( $get_first_pic && $key == 0 )
			{
				$thumb_pic = ( $this->m_images->resize_images( $name ) )?$name:'';
				if( !$down_pic )
				{
					return array( 'thumb_pic' => $thumb_pic, 'content' => $str );
				}
			}
			//加水印
			if( $validate && $config['watermark'] && $config['watermark_type'] == 'img' )
			{
				$this->m_images->watermark_img( $name, $config['watermark_img'] );
			}
			elseif( $validate && $config['watermark'] && $config['watermark_type'] == 'word' )
			{
				$this->m_images->watermark_word( $name, $config['watermark_word'] );
			}
			//图片路径替换
			if( $validate )
			{
				$new_url = SITE_URL.'uploads/images/'.$name;
				$str = str_replace( $value, $new_url, $str );
			}
			unset( $name_ext, $name_exts, $name, $validate );
		}
		return array( 'thumb_pic' => $thumb_pic, 'content' => $str );
	}
	
	/**
	 * 进行分页
	 *
	 * @param string $string
	 * @param int $type
	 * @return array
	 */
	function explode_content( $string )
	{
		$contents = explode( '[-page-]', stripslashes( $string ) );
		foreach( $contents AS $key => $value)
		{
			$datas[$key] = addslashes( $value );
		}
		return $datas;
	}
	
	/**
	 * 删除文章
	 *
	 * @param string $ids
	 * @return bool
	 */
	function delete( $ids )
	{
		$id_arrays = explode( '_', $ids );
		$i = 0;
		foreach( $id_arrays as $v )
		{
			if( !empty( $v ) )
			{
				$this->del_one_article( $v )?'':$i = 1;
			}
		}
		return ( $i == 0 )?true:false;
	}
	
	/**
	 * 将文章移入回收站
	 *
	 * @param string $ids
	 * @return bool
	 */
	function recycle( $ids )
	{
		$id_arrays = explode( '_', $ids );
		$i = 0;
		foreach( $id_arrays as $v )
		{
			if( !empty( $v ) )
			{
				$this->db->update( DB_PRE.'article', array( 'recycle' => 1 ), array( 'id' => $v ) )?'':$i = 1;
			}
		}
		return ( $i == 0 )?true:false;
	}
	
	/**
	 * 发布文章
	 *
	 * @param string $ids
	 * @return bool
	 */
	function post( $ids )
	{
		$id_arrays = explode( '_', $ids );
		$i = 0;
		foreach( $id_arrays as $v )
		{
			if( !empty( $v ) )
			{
				$this->db->update( DB_PRE.'article', array( 'draft' => 0 ), array( 'id' => $v ) )?'':$i = 1;
			}
		}
		return ( $i == 0 )?true:false;
	}
	
	/**
	 * 从回收站还原文章
	 *
	 * @param string $ids
	 * @return bool
	 */
	function restore( $ids )
	{
		$id_arrays = explode( '_', $ids );
		$i = 0;
		foreach( $id_arrays as $v )
		{
			if( !empty( $v ) )
			{
				$this->db->update( DB_PRE.'article', array( 'recycle' => 0 ), array( 'id' => $v ) )?'':$i = 1;
			}
		}
		return ( $i == 0 )?true:false;
	}
	
	/**
	 * 审核文章
	 *
	 * @param string $ids
	 * @return bool
	 */
	function validate( $ids, $type )
	{
		$id_arrays = explode( '_', $ids );
		$i = 0;
		foreach( $id_arrays as $v )
		{
			if( !empty( $v ) )
			{
				$this->db->update( DB_PRE.'article', array( 'validate' => $type ), array( 'id' => $v ) )?'':$i = 1;
			}
		}
		return ( $i == 0 )?true:false;
	}
	
	/**
	 * 推荐文章
	 *
	 * @param string $ids
	 * @return bool
	 */
	function recommend( $ids )
	{
		$id_arrays = explode( '_', $ids );
		$i = 0;
		foreach( $id_arrays as $v )
		{
			if( !empty( $v ) )
			{
				$this->db->update( DB_PRE.'article', array( 'recommend' => 1 ), array( 'id' => $v ) )?'':$i = 1;
			}
		}
		return ( $i == 0 )?true:false;
	}
	
	/**
	 * 取消推荐文章
	 *
	 * @param string $ids
	 * @return bool
	 */
	function unrecommend( $ids )
	{
		$id_arrays = explode( '_', $ids );
		$i = 0;
		foreach( $id_arrays as $v )
		{
			if( !empty( $v ) )
			{
				$this->db->update( DB_PRE.'article', array( 'recommend' => 0 ), array( 'id' => $v ) )?'':$i = 1;
			}
		}
		return ( $i == 0 )?true:false;
	}
	
	/**
	 * 移动文章
	 *
	 * @param string $ids
	 * @param int $column_id
	 * @return bool
	 */
	function move( $ids, $column_id )
	{
		$id_arrays = explode( '_', $ids );
		$i = 0;
		foreach( $id_arrays as $v )
		{
			if( !empty( $v ) )
			{
				$this->db->update( DB_PRE.'article', array( 'cid' => $column_id ), array( 'id' => $v ) )?'':$i = 1;
			}
		}
		return ( $i == 0 )?true:false;
	}
	
}